package model.hall
{
	import core.Model;
	
	import events.HallEvent;
	import events.LoginEvent;
	
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	import global.GameState;
	
	import model.load.XmlLoadMessage;
	
	public class HallModel extends EventDispatcher
	{
		
		private var _model:Model = Model.getInstance();
		private var _roomXml:XML;
		private var _roomXmls:Dictionary = new Dictionary();
		//private var lastXmlMsg:XmlLoadMessage;
		private var currentType:int;
		
		public function HallModel()
		{
			initListener();
		}
		
		private function initListener():void
		{
			_model.loginModel.addEventListener(LoginEvent.SUCCESS,onLoginSuccess);
		}
		
		private function onLoginSuccess(event:LoginEvent):void
		{
			if (GameState.DEBUG_STATE == 0)
			{
				onPlayerInfoBack(GameState.playerInfoXml,0);
			}
			else
			{
				loadPlayerInfo();
			}
		}
		
		public function sendRoomListRequest(id:int):void
		{
			/*if (lastXmlMsg)
			{
				lastXmlMsg.cancel();
			}*/
			currentType = id;
			if (_roomXmls[id] != null)
			{
				onXmlLoadBack(_roomXmls[id],id);
				return;
			}
			
			var url:String = GameState.getServerLink() + "14&roomLevel="+ id +"&key=" + GameState.snsKey;
			var xmlLoadMessage:XmlLoadMessage = new XmlLoadMessage(onXmlLoadBack);
			xmlLoadMessage.url = url;
			xmlLoadMessage.id = id;
			xmlLoadMessage.send();
			
			//lastXmlMsg = xmlLoadMessage;
			
			
		}
		
		public function clearRoomXML(id:int):void
		{
			_roomXmls[id] = null;
		}
		
		private function onXmlLoadBack(xml:XML,id:int):void
		{
			_roomXmls[id] = xml;
			if (id == currentType)
			{
				var event:HallEvent = new HallEvent(HallEvent.ROOM_XML_COMPLETE);
				event.xmlType = id;
				event.xml = xml;
				_roomXml = xml;
				dispatchEvent(event);
			}
		}
		
		public function seach(key:String):void
		{
			var result:XML = new XML("<main></main>");
			if (key == "")
			{
				result = _roomXml;
			}
			else
			{
				if (_roomXml)
				{
					for each (var data:XML in _roomXml.children())
					{
						if (data.@name.toString().indexOf(key) >= 0)
						{
							result.appendChild(data);
						}
					}
				}
			}
			var event:HallEvent;
			
			if (result.children().length() <= 0)
			{
				event = new HallEvent(HallEvent.SEARCH_NONE);
			}
			else
			{
				event = new HallEvent(HallEvent.SEARCH_COMPLETE);
				event.xml = result;
				
			}
			dispatchEvent(event);
		}
		
		public function get roomListXml():XML
		{
			if (_roomXml)
			{
				return _roomXml.copy();
			}
			return null;
		}
		
		public function loadPlayerInfo():void
		{
			var url:String = GameState.getServerLink() + "16&key=" + GameState.snsKey;
			var xmlLoadMessage:XmlLoadMessage = new XmlLoadMessage(onPlayerInfoBack);
			xmlLoadMessage.url = url;
			xmlLoadMessage.send();
		}
		
		private function onPlayerInfoBack(xml:XML,id:int):void
		{
			var event:HallEvent = new HallEvent(HallEvent.PLAYER_INFO_COMPLETE);
			event.xml = xml;
			dispatchEvent(event);
		}
		
		public function sendRoomPlayerListRequest(serverId:String,roomId:String,type:int):void
		{
			var url:String = GameState.getServerLink() + "15&serverId=" + serverId + "&roomId=" +roomId+ "&key=" + GameState.snsKey;
			var xmlLoadMessage:XmlLoadMessage = new XmlLoadMessage(onRoomPlayerListBack);
			xmlLoadMessage.url = url;
			xmlLoadMessage.id = type;
			xmlLoadMessage.send();
		}
		
		private function onRoomPlayerListBack(xml:XML,id:int):void
		{
			var event:HallEvent = new HallEvent(HallEvent.ROOM_PLAYER_LIST_COMPLETE);
			event.xml = xml;
			event.xmlType = id;
			dispatchEvent(event);
		}
		
		public function sendFriendListRequest(type:int):void
		{
			var url:String  = GameState.getServerLink() + "21&type=" + type+ "&key=" + GameState.snsKey;
			var xmlLoadMessage:XmlLoadMessage = new XmlLoadMessage(onRoomPlayerListBack);
			xmlLoadMessage.url = url;
			xmlLoadMessage.id = type;
			xmlLoadMessage.send();
		}
		
		public function onFriendListBack(xml:XML,id:int):void
		{
			var event:HallEvent = new HallEvent(HallEvent.FRIEND_LIST_COMPLETE);
			event.xml = xml;
			event.xmlType = id;
			dispatchEvent(event);
		}
		
	}
}